package com.cw.subject.domain.service.impl;

import com.alibaba.fastjson.JSON;
import com.cw.subject.common.entity.PageResult;
import com.cw.subject.common.enums.IsDeletedFlagEnum;
import com.cw.subject.domain.convert.SubjectInfoConverter;
import com.cw.subject.domain.entity.SubjectInfoBO;
import com.cw.subject.domain.entity.SubjectOptionBO;
import com.cw.subject.domain.handler.subject.SubjectTypeHandler;
import com.cw.subject.domain.handler.subject.SubjectTypeHandlerFactory;
import com.cw.subject.domain.service.SubjectInfoDomainService;
import com.cw.subject.infra.basic.entity.SubjectInfoPO;
import com.cw.subject.infra.basic.entity.SubjectLabelPO;
import com.cw.subject.infra.basic.entity.SubjectMappingPO;
import com.cw.subject.infra.basic.service.SubjectInfoPOService;
import com.cw.subject.infra.basic.service.SubjectLabelPOService;
import com.cw.subject.infra.basic.service.SubjectMappingPOService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class SubjectInfoDomainServiceImpl implements SubjectInfoDomainService {

    @Resource
    private SubjectInfoPOService subjectInfoPOService;
    @Resource
    private SubjectTypeHandlerFactory subjectTypeHandlerFactory;
    @Resource
    private SubjectMappingPOService subjectMappingPOService;
    @Resource
    private SubjectLabelPOService subjectLabelPOService;

    /**
     * @author: 江传文
     * @date: 2024/4/13 19:06
     * 新增题目
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(SubjectInfoBO subjectInfoBO) {
        if (log.isInfoEnabled()) {
            log.info("SubjectInfoDomainServiceImpl.add.bo:{}", JSON.toJSONString(subjectInfoBO));
        }
        SubjectInfoPO subjectInfoPO = SubjectInfoConverter.INSTANCE.convertBoToInfoPO(subjectInfoBO);
        subjectInfoPO.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode()); // 设置为未删除
        subjectInfoPOService.insert(subjectInfoPO);

        SubjectTypeHandler handler = subjectTypeHandlerFactory.getHandler(subjectInfoPO.getSubjectType());
        subjectInfoBO.setId(subjectInfoPO.getId());
        handler.add(subjectInfoBO);

        List<Integer> categoryIds = subjectInfoBO.getCategoryIds();
        List<Integer> labelIds = subjectInfoBO.getLabelIds();
        List<SubjectMappingPO> mappingList = new LinkedList<>();
        categoryIds.forEach(categoryId -> {
            labelIds.forEach(labelId -> {
                SubjectMappingPO subjectMappingPO = new SubjectMappingPO();
                subjectMappingPO.setSubjectId(subjectInfoPO.getId());
                subjectMappingPO.setCategoryId(categoryId);
                subjectMappingPO.setLabelId(labelId);
                subjectMappingPO.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
                mappingList.add(subjectMappingPO);
            });
        });
        subjectMappingPOService.batchInsert(mappingList);
    }

    /**
     * @author: 江传文
     * @date: 2024/4/13 19:06
     * 查询题目列表
     */
    @Override
    public PageResult<SubjectInfoBO> getSubjectPage(SubjectInfoBO subjectInfoBO) {
        PageResult<SubjectInfoBO> pageResult = new PageResult<>();
        pageResult.setPageNo(subjectInfoBO.getPageNo());
        pageResult.setPageSize(subjectInfoBO.getPageSize());
        int start = (subjectInfoBO.getPageNo() - 1) * subjectInfoBO.getPageSize();
        SubjectInfoPO subjectInfoPO = SubjectInfoConverter.INSTANCE.convertBoToInfoPO(subjectInfoBO);
        int count = subjectInfoPOService.countByCondition(subjectInfoPO,
                subjectInfoBO.getCategoryId(),
                subjectInfoBO.getLabelId()); // 为了不污染实体,暂且这样做
        if (count == 0){
            return pageResult;
        }
        List<SubjectInfoPO> subjectInfoPOList = subjectInfoPOService.queryPage(subjectInfoPO,subjectInfoBO.getCategoryId(),
                subjectInfoBO.getLabelId(),start,subjectInfoBO.getPageSize());
        List<SubjectInfoBO> subjectInfoBOList = SubjectInfoConverter.INSTANCE.convertListInfoToBO(subjectInfoPOList);
        pageResult.setRecords(subjectInfoBOList);
        pageResult.setTotal(count);
        return pageResult;
    }

    /**
     * @author: 江传文
     * @date: 2024/4/13 19:06
     * 查询题目信息
     */
    @Override
    public SubjectInfoBO querySubjectInfo(SubjectInfoBO subjectInfoBO) {
        SubjectInfoPO subjectInfoPO = subjectInfoPOService.queryById(subjectInfoBO.getId());
        SubjectTypeHandler handler = subjectTypeHandlerFactory.getHandler(subjectInfoPO.getSubjectType());
        SubjectOptionBO optionBO = handler.query(subjectInfoPO.getId());
        SubjectInfoBO bo = SubjectInfoConverter.INSTANCE.convertOptionAndInfoToBo(optionBO, subjectInfoPO);
        // label相关信息获取
        SubjectMappingPO subjectMappingPO = new SubjectMappingPO();
        subjectMappingPO.setSubjectId(subjectInfoPO.getId());
        subjectMappingPO.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
        List<SubjectMappingPO> mappingPOList = subjectMappingPOService.queryLabelId(subjectMappingPO);
        List<Integer> labelIdList = mappingPOList.stream().map(SubjectMappingPO::getLabelId).collect(Collectors.toList());
        List<SubjectLabelPO> labelPOList = subjectLabelPOService.batchQueryById(labelIdList);
        List<String> labelNameList = labelPOList.stream().map(SubjectLabelPO :: getLabelName).collect(Collectors.toList());
        bo.setLabelName(labelNameList);
        return bo;
    }
}
